/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import java.io.PrintStream;
/*     */ import java.security.InvalidKeyException;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.TreeSet;
/*     */ import javax.crypto.Mac;
/*     */ import javax.crypto.SecretKey;
/*     */ import org.apache.commons.codec.binary.Hex;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PreAuthKey
/*     */ {
/*     */   public static final int KEY_SIZE_BYTES = 32;
/*     */   
/*     */   public static String generateRandomPreAuthKey()
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/*  47 */       SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
/*  48 */       byte[] key = new byte[32];
/*  49 */       random.nextBytes(key);
/*  50 */       return new String(Hex.encodeHex(key));
/*     */     } catch (NoSuchAlgorithmException e) {
/*  52 */       throw ServiceException.FAILURE("unable to initialize SecureRandom", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public static String computePreAuth(Map<String, String> params, String key) {
/*  57 */     TreeSet<String> names = new TreeSet(params.keySet());
/*  58 */     StringBuilder sb = new StringBuilder();
/*  59 */     for (String name : names) {
/*  60 */       if (sb.length() > 0) sb.append('|');
/*  61 */       sb.append((String)params.get(name));
/*     */     }
/*  63 */     return getHmac(sb.toString(), key.getBytes());
/*     */   }
/*     */   
/*     */   private static String getHmac(String data, byte[] key) {
/*     */     try {
/*  68 */       ByteKey bk = new ByteKey(key);
/*  69 */       Mac mac = Mac.getInstance("HmacSHA1");
/*  70 */       mac.init(bk);
/*  71 */       return new String(Hex.encodeHex(mac.doFinal(data.getBytes())));
/*     */     } catch (NoSuchAlgorithmException e) {
/*  73 */       throw new RuntimeException("fatal error", e);
/*     */     } catch (InvalidKeyException e) {
/*  75 */       throw new RuntimeException("fatal error", e);
/*     */     }
/*     */   }
/*     */   
/*     */   static class ByteKey implements SecretKey {
/*     */     private byte[] mKey;
/*     */     
/*     */     ByteKey(byte[] key) {
/*  83 */       this.mKey = ((byte[])key.clone());
/*     */     }
/*     */     
/*     */     public byte[] getEncoded() {
/*  87 */       return this.mKey;
/*     */     }
/*     */     
/*     */     public String getAlgorithm() {
/*  91 */       return "HmacSHA1";
/*     */     }
/*     */     
/*     */     public String getFormat() {
/*  95 */       return "RAW";
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws ServiceException
/*     */   {
/* 101 */     long now = System.currentTimeMillis();
/* 102 */     HashMap<String, String> params = new HashMap();
/* 103 */     params.put("account", "user1");
/* 104 */     params.put("by", "name");
/* 105 */     params.put("timestamp", "1176399950434");
/* 106 */     params.put("expires", "0");
/* 107 */     String key = "9d8ad87fd726ba7d5fecf3d705621024b31cedb142310ec965f9263568fa0f27";
/* 108 */     System.out.printf("key=%s preAuth=%s\n", new Object[] { key, computePreAuth(params, key) });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/PreAuthKey.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */